function RiskAlarmMan() {
	var currentNum = 1;
	var divId = 'affectObjDiv';
	var tables = undefined;
	var data = {};
	// var stationPoints=[];
	var pops = new Map();
	this.openPop = function(currentFeature) {
		var popId = currentFeature.data.value.station.id + 'pop';
		var type = currentFeature.data.type;
		var pops = this.getPops();
		var pop = pops.get(popId);
		if (pop == null) {
			this.drawPopUp(type, currentFeature.data.value);
		} else {
			pop.remove();
			pops.remove(popId);
			//map.removePopup(pop);
		}
	};
	this.setDeviceType = function(index) {
		currentNum = index;
		return this;
	};
	this.show = function() {
		// clearAll();
		getData();
	};
	this.hide = function() {
		if (tables) {
			tables.clearAndHide();
		}
	};
	this.getPops = function() {
		return pops;
	}
	function show() {
		// clearAll();
		tables = new PanelTablesUtil(divId);
		tables.setInfo(queryDiv.getComponent);
		drawPanelTables();
		drawAllIcons();
	}
	function getData() {
		var alarmUrl = '';
		var expUrl = "";
		if (currentNum == 1) {
			alarmUrl = '/rest/riskwarning/getPrimaryRiskAlarmMessage';// 实时告警
		} else {
			alarmUrl = '/rest/riskwarning/getSecondaryAlarmMessage';
		}
		// 告警数据
		$.getJSON(alarmUrl, function(result) {
			data[currentNum] = result;
			show();
		});
	}
	function getDataByTime(start, end) {
		var alarmUrl = '';
		var expUrl = "";
		if (currentNum == 1) {
			var url = '/ui/map/primaryAlarmDetail.xhtml?start=' + start
					+ '&end=' + end;
			MyModal.showModal(url, null, null, '风险告警详细信息');
		} else {
			var url = '/ui/map/secondaryAlarmDetail.xhtml?start=' + start
					+ '&end=' + end;
			MyModal.showModal(url, null, null, '风险告警详细信息');
		}
	}

	this.getPrimeStationByType = function(clickObj) {
		var type = $(clickObj).children("a").html();
		$("#riskDiv li").removeClass("riskSelected");
		$(clickObj).addClass("riskSelected");
		$.getJSON("/rest/primerisk/getPrimeRiskStationByType/" + type,
				function(result) {
					weatherLayer.removeAllFeatures();
					weatherLayer.setVisibility(false);
					selectedLayer.removeAllFeatures();
					pieLayer.removeAllFeatures();
					pieLayer.setVisibility(false);
					var ss = result;
					var pointVectors = [];
					for ( var i in ss) {
						pointVectors.push(riskAlarmMan.drawRiskStation(ss[i],
								"primeType" + paramMap[type]));
					}
					selectFeature.activate();
					selectedLayer.addFeatures(pointVectors);
				});
	}

	this.getSecondStationByType = function(clickObj) {
		var type = $(clickObj).children("a").html();
		$("#riskDiv li").removeClass("riskSelected");
		$(clickObj).addClass("riskSelected");
		$.getJSON("/rest/SecRiskEvaluateService/getSecondRiskStationByType/"
				+ type, function(result) {
			weatherLayer.removeAllFeatures();
			weatherLayer.setVisibility(false);
			searchObjLayer.removeAllFeatures();
			selectedLayer.removeAllFeatures();
			pieLayer.removeAllFeatures();
			pieLayer.setVisibility(false);
			var ss = result;
			var pointVectors = [];
			for ( var i in ss) {
				pointVectors.push(riskAlarmMan.drawRiskStation(ss[i],
						"secondType" + paramMap[type]));
			}
			selectFeature.activate();
			selectedLayer.addFeatures(pointVectors);
		});
	}
	// 安全评估
	this.drawRiskStation = function(ss, type) {
		// 点对象
		var point = new SuperMap.Geometry.Point(ss.station.longitude,
				ss.station.latitude);
		var pointVector = new SuperMap.Feature.Vector(point);
		pointVector.style = {
			fillColor : ss.style,
			strokeColor : "green",
			pointRadius : 9
		};
		pointVector.data.type = type;
		pointVector.data.value = ss;
		return pointVector;
	}
	// 画全部站点，不分类型
	function drawAllIcons() {
		var sdata = data[currentNum].stations;
		var points = [];
		var gif;
		for ( var i in sdata) {
			if (sdata[i].specialSt == "1") {
				gif = '/resources/img/dangerAlarm.gif';
			} else {
				gif = '/resources/img/daily/fault.gif';
			}
			if (sdata[i].station) {
				points.push(drawIcon(sdata[i], gif, "All"));
			}
		}
		weatherLayer.addFeatures(points);
		weatherLayer.setVisibility(true);
		selectFeature.activate();
	}
	// 画不同类型的站点(区分特维站点)
	function drawIcons(type) {
		var sdata = data[currentNum].stations;
		if (!sdata) {
			return;
		}
		var points = [];
		var gif = '/resources/img/daily/fault.gif';
		for ( var i in sdata) {
			if (sdata[i].specialSt == "1") {
				gif = '/resources/img/dangerAlarm.gif';
			} else {
				gif = '/resources/img/daily/fault.gif';
			}
			if (sdata[i].msgs[type] && sdata[i].station) {
				points.push(drawIcon(sdata[i], gif, type));
				// riskAlarmMan.drawPopUp(type,sdata[i].station);
			}
		}
		weatherLayer.addFeatures(points);
		weatherLayer.setVisibility(true);
		selectFeature.activate();
	}
	this.drawPopUp = function(msgType, stat) {
		var pop = new Popup(stat.station.longitude, stat.station.latitude);
		pop.setTitle(stat.station.name);
		msgType = msgType.replace("riskAlarmPoint", "");
		switch (msgType) {
		case "All":
			for ( var type in stat.msgs) {
				for ( var i in stat.msgs[type]) {
					var msg = stat.msgs[type][i];
					pop.setRowContent(msg.content.split("/")[1]);
					pop.setRowIconClickListener("riskAlarmMan.onRowIconClick('"
							+ msg.msgType + "','" + msg.source + "','" + msg.id
							+ "')");
					pop.setRowSelfClickListener("riskAlarmMan.onRowIconClick('"
							+ msg.msgType + "','" + msg.source + "','" + msg.id
							+ "')");
				}
			}
			break;
		default:
			if (stat.msgs[msgType]) {
				for ( var i in stat.msgs[msgType]) {
					var msg = stat.msgs[msgType][i];
					pop.setRowContent(msg.content.split("/")[1]);
					pop.setRowIconClickListener("riskAlarmMan.onRowIconClick('"
							+ msg.msgType + "','" + msg.source + "','" + msg.id
							+ "')");
					pop.setRowSelfClickListener("riskAlarmMan.onRowIconClick('"
							+ msg.msgType + "','" + msg.source + "','" + msg.id
							+ "')");
				}
			}
		}
		pop.showCloseIcon(true);
		pop.show();
		pops.put(stat.station.id + "pop", pop);
	}
	this.onRowIconClick = function(msgType, sourceId, id) {
		switch (msgType) {
		case "ctbh":
			MyModal
					.showModal("/ui/map/dialog/riskAlarmDialog/ctsaReportDlg.xhtml?oId="
							+ sourceId + "&msgId=" + id);
			break;
		case "dlqyc":
		case "gzdlgldyc":
		case "xhsjyc":
			MyModal
					.showModal("/ui/map/dialog/riskAlarmDialog/breakerActException.xhtml?recordId="
							+ sourceId + "&msgType=" + msgType + "&msgId=" + id);
			break;
		case "hlyc":
			MyModal
					.showModal("/ui/map/dialog/riskAlarmDialog/protectDeviceException.xhtml?oId="
							+ sourceId + "&msgId=" + id);
			break;
		case "bhxyyc":
			$
					.getJSON(
							"/rest/riskwarning/getAffectedObjectByTypeAndOid/"
									+ id + "/bhxyyc/" + sourceId,
							function(result) {
								if (result != null) {
									MyModal
											.showModal("/ui/map/protectActSvg/protectActSvg.xhtml?lineId="
													+ result.objectId
													+ "&faultTime="
													+ result.faultTime
													+ "&lineName="
													+ result.objectName);
								}
							})
			break;
		case "tdyc_single":
		case "tdyc_double":
			MyModal
					.showModal("/ui/map/dialog/riskAlarmDialog/protectChannelException.xhtml?oId="
							+ sourceId + "&msgId=" + id);
			break;
		case "tzgj":
			MyModal
					.showModal("/ui/map/dialog/riskAlarmDialog/tripException.xhtml?oId="
							+ sourceId + "&msgId=" + id);
			break;
		}
	};
	function drawIcon(s, gif, type) {
		if (s.station) {
			var point = new SuperMap.Geometry.Point(s.station.longitude,
					s.station.latitude);
			var gifPoint = new SuperMap.Feature.Vector(point, null, {
				externalGraphic : gif,
				graphicWidth : 35,
				graphicHeight : 35,
				graphicOpacity : 1,
				label : s.station.name + "",
				labelXOffset : '-3',
				labelYOffset : '-25',
				fontColor : "black",
				fontOpacity : "1",
				fontSize : "15px",
				fontWeight : "bold",
				labelSelect : "true"
			});
			gifPoint.data.type = "riskAlarmPoint" + type;
			gifPoint.data.value = s;
			return gifPoint;
		}
	}
	this.onClickTbHeader = function(tbObj) {
		var popArray = pops.values();
		for ( var i in popArray) {
			popArray[i].remove();
		}
		pops.clear();
		weatherLayer.removeAllFeatures();
		selectedLayer.removeAllFeatures();
		searchObjLayer.removeAllFeatures();
		var headerTxt = $(tbObj).html();
		drawIcons(headerTxt);
	}
	function drawPanelTables() {
		var tdata = data[currentNum].alarms;
		tables.clear();
		var title = currentNum == 1 ? '一次设备' : '二次设备';
		tables.setTitle(title);
		tables.setTitleTool("<i class='fa fa-search'></i>",
				"riskAlarmMan.searchAlarmByCondition()", "searchAlarmBtn");
		var i = 0;
		for ( var type in tdata) {
			i++;
			var d = tdata[type];
			if (d.length > 0) {
				var tb = tables.drawCollapseTable("riskAlarm" + type, type,
						false, 'riskAlarmMan.onClickTbHeader(this)');

				drawTable(tb, getDataset(d));
			} else {
				tables.getCollapsePanelEle("riskAlarm" + type + "parent", type,
						false)
						.append('<span style="text-align:center">无<span>');
			}
		}
		if (i == 0) {
			tables
					.setInfo('<div style="height:50px;text-align:center;font-size:20px;margin:20px">无告警数据。<div>');
			$('#' + divId).removeClass('box-danger');
			$('#' + divId).addClass('box-success');
		} else {
			$('#' + divId).removeClass('box-success');
			$('#' + divId).addClass('box-danger');
		}
		tables.show();
	}
	//弹出查询告警消息页面
	this.searchAlarmByCondition = function() {
		MyModal
				.showModal("/ui/map/dialog/riskAlarmDialog/searchAlarmMsgDlg.xhtml");
	}
	function getDataset(data) {
		var dataset = [];
		for ( var i in data) {
			var d = data[i];
			dataset.push([ getTimeStr(d['createTime']), d['content'],
					d['msgType'], d['source'], d['id'] ]);
		}
		return dataset;
	}
	function getTimeStr(time) {
		var t = new Date(time.time);
		return t.getFullYear() + "-" + doubleNum(t.getMonth() + 1) + "-"
				+ doubleNum(t.getDate()) + " " + doubleNum(t.getHours()) + ":"
				+ doubleNum(t.getMinutes()) + ":" + doubleNum(t.getSeconds());
	}
	function doubleNum(num) {
		return num > 9 ? num : '0' + num;
	}
	function drawTable(tb, dataset) {
		var opt = MyTable.getDefaultOptions();
		opt.scrollY = 300;
		opt.scrollCollapse = true;
		opt.autoWidth = false;
		opt.columnDefs = [ {
			targets : [ 2, 3, 4 ],
			visible : false
		}, {
			targets : 0,
			width : '30%'
		}, {
			targets : 1,
			width : '70%'
		} ];
		var table = new MyTable(tb, [ '时间', '内容' ], opt);
		table.setDataSet(dataset);
		table.show();
		table.onRowClick(function(data) {
			var msgId = data[4];
			var msgType = data[2];
			var msgOid = data[3];
			selectedLayer.removeAllFeatures();
			searchObjLayer.removeAllFeatures();
			if (msgOid == "") {
				//无sourceId,直接打开详情弹窗
				openDetailDlgByMsgType(msgType, msgId, msgOid);
			} else {
				$.getJSON("/rest/riskwarning/getAffectedObjectByTypeAndOid/"
						+ msgId + "/" + msgType + "/" + msgOid,
						function(result) {
							if (result != null) {
								$("#input_search").val(result.objectName);
								searchObj(true, true);
								openDetailDlgByMsgType(msgType, msgId, msgOid);
							} else {
								var rowIndex = table.getTable()
										.row('.selected').index();
								dataset.splice(rowIndex, 1);
								table.getTable().row('.selected').remove()
										.draw();
							}
						});
			}
		});
	}
	//根据消息类型打开相应的详情弹窗
	function openDetailDlgByMsgType(msgType, msgId, msgOid) {
		switch (msgType) {
		case "ctbh":
			pageUrl = "/ui/map/dialog/riskAlarmDialog/ctsaReportDlg.xhtml?oId="
					+ msgOid + "&msgId=" + msgId;
			break;
		case "dlqyc":
		case "gzdlgldyc":
		case "xhsjyc":
			pageUrl = "/ui/map/dialog/riskAlarmDialog/breakerActException.xhtml?recordId="
					+ msgOid + "&msgType=" + msgType + "&msgId=" + msgId;
			break;
		case "hlyc":
			pageUrl = "/ui/map/dialog/riskAlarmDialog/protectDeviceException.xhtml?oId="
					+ msgOid + "&msgId=" + msgId;
			break;
		case "bhxyyc":
			$
					.getJSON(
							"/rest/riskwarning/getAffectedObjectByTypeAndOid/"
									+ msgId + "/bhxyyc/" + msgOid,
							function(result) {
								if (result != null) {
									pageUrl = "/ui/map/protectActSvg/protectActSvg.xhtml?lineId="
											+ result.objectId
											+ "&faultTime="
											+ result.faultTime
											+ "&lineName="
											+ result.objectName;
								}
							})
			break;
		case "tdyc_single":
		case "tdyc_double":
			pageUrl = "/ui/map/dialog/riskAlarmDialog/protectChannelException.xhtml?oId="
					+ msgOid + "&msgId=" + msgId;
			break;
		case "tzgj":
			pageUrl = "/ui/map/dialog/riskAlarmDialog/tripException.xhtml?oId="
					+ msgOid + "&msgId=" + msgId;
		}
		MyModal.showModal(pageUrl);
	}

	/**
	 * 查询div的组装、添加查询时间等
	 */
	var queryDiv = (function() {
		var component, restore, query, error, minus, start, end;
		var pattern = 'yyyy-MM-dd';
		function getComponent() {
			component = $('<div class="panel-collapse collapse in" style="background:white" />');
			var now = new Date();
			var last = new Date(now.getTime() - 86400000);//一天前的时间戳
			var d = $('<div style="display:flex;"/>').appendTo(component);
			minus = $(
					'<span class="btn btn-default" ><i class="fa fa-arrow-left"></i></span>')
					.appendTo(d);
			start = $(
					'<input type="text" class="form-control thunder-warning-start"/>')
					.appendTo(d);
			end = $(
					'<input type="text" class="form-control thunder-warning-end"/>')
					.appendTo(d);
			// restore = $('<span class="btn btn-default" ><i class="fa
			// fa-repeat"></i></span>').appendTo(d);
			query = $('<span class="btn btn-default" >搜索</span>').appendTo(d);

			start.val(DateUtil.pattern(last, pattern));
			end.val(DateUtil.pattern(now, pattern));
			addListener();
			// start.val('2018-03-12 00:00');
			// end.val('2018-03-15 00:00');
			return component;
		}
		function addListener() {
			var now = new Date();
			var last = new Date(now.getTime() - 7 * 24 * 3600 * 1000);
			var startTime = DateUtil.pattern(last, pattern);
			var endTime = DateUtil.pattern(now, pattern);
			minus.click(function() {
				var str = start.val();
				var d = new Date(str);
				d.setTime(d.getTime() - 24 * 3600 * 1000);
				start.val(DateUtil.pattern(d, pattern));
			});
			query.click(function() {
				// 将空的和不对的补齐
				var sta = start.val();
				var s = new Date(sta);
				if (!sta || s.toString() == 'Invalid Date') {
					sta = startTime;
					$('.thunder-warning-start').val(startTime);
					s = new Date(sta);
				}
				var en = $('.thunder-warning-end').val();
				var e = new Date(en);
				if (!en || e.toString() == 'Invalid Date') {
					en = endTime;
					$('.thunder-warning-end').val(endTime);
					e = new Date(en);
				}
				// 判断是否在允许范围内
				startTime = sta;
				endTime = en;
				//TODO 添加查询方法
				getDataByTime(startTime, endTime);
			});
			start.datetimepicker({
				format : 'yyyy-mm-dd',
				minView : 2,
				autoclose : true,
				endDate : new Date(),
				todayHighlight : true,
			});
			end.datetimepicker({
				format : 'yyyy-mm-dd',
				minView : 2,
				autoclose : true,
				endDate : new Date(),
				todayHighlight : true,
				pickerPosition : 'bottom-left',
			});
		}
		function getQueryTime() {
			return [ start.val(), end.val() ];
		}
		return {
			getComponent : getComponent,
			addListener : addListener,
			getQueryTime : getQueryTime
		};
	})();
}